package com.lh_travel.lohas.base;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lh_travel.lohas.domain.Record;
import com.lh_travel.lohas.domain.UserBean;
import com.lh_travel.lohas.utils.ScrPtHelper;
import com.lh_travel.lohas.widget.NestedScrollWebView;
import com.mob.MobSDK;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by aaron on 2018/5/23.
 */

public class MainApplication extends Application {
    private SharedPreferences mPrefs;
    public boolean log = false;
    public String path;
    public NestedScrollWebView mWebView;

    @Override
    public void onCreate() {
        super.onCreate();

        //设计图标注的宽度
        int designWidth = 750;

        new ScrPtHelper(this, designWidth).activate();

        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        mWebView = new NestedScrollWebView(getApplicationContext());

        MobSDK.init(this);
    }

    public SharedPreferences getPref() {
        return mPrefs;
    }

    public void setPreference(String PrefId, boolean value) {
        if (false == value) {
            mPrefs.edit().remove(PrefId).commit();
        } else {
            mPrefs.edit().putBoolean(PrefId, value).commit();
        }
    }

    public void setPreference(String PrefId, String value) {
        if (TextUtils.isEmpty(value)) {
            mPrefs.edit().remove(PrefId).commit();
        } else {
            mPrefs.edit().putString(PrefId, value).commit();
        }
    }

    public String getPreference(String PrefId) {

        String value = mPrefs.getString(PrefId, null);
        if (TextUtils.isEmpty(value)) {
            return null;
        }
        return value;
    }

    public void setListPrefernce(String PrefId, String value, String belong) {

        List<Record> dataList = getListPrefernce(PrefId);
        Record record = new Record(value, belong);
        Gson gson = new Gson();

        if (!dataList.contains(record)) {

            dataList.add(0, record);

            if (dataList.size() == 9)
                dataList.remove(dataList.size() - 1);

            String strJson = gson.toJson(dataList);

            mPrefs.edit().putString(PrefId, strJson).commit();

        } else {

            int i = dataList.indexOf(record);

            synchronized (this) {
                if (i > 0) {
                    synchronized (this) {

                        if (i > 0) {
                            dataList.remove(i);

                            dataList.add(0, record);

                            if (dataList.size() == 9)
                                dataList.remove(dataList.size() - 1);

                            String strJson = gson.toJson(dataList);

                            mPrefs.edit().putString(PrefId, strJson).commit();
                        }
                    }
                }
            }
        }

    }

    public List<Record> getListPrefernce(String PrefId) {
        List<Record> dataList = new ArrayList<>();

        String json = mPrefs.getString(PrefId, null);

        if (json == null) {
            return dataList;
        }

        Gson gson = new Gson();

        dataList = gson.fromJson(json, new TypeToken<List<Record>>() {
        }.getType());

        return dataList;
    }

    public UserBean getBeanPrefernce(String PrefId){

        String json = mPrefs.getString(PrefId, null);

        if (json == null){
            return null;
        }

        Gson gson = new Gson();
        UserBean bean = gson.fromJson(json, UserBean.class);

        return bean;
    }

    public void setBeanPrefernce(String PrefId, UserBean bean){

        Gson gson = new Gson();
        String jsonStr=gson.toJson(bean);

        mPrefs.edit().putString(PrefId, jsonStr).commit();

    }
}
